<?php

namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
//        $router->model('post', 'App\Post');

        /**
         * 所谓的路由模型绑定, 其实绑定的是路由分发中参数
         * Route::get('test/route-model/{post}', 'Test\TestsController@getRouteModel');
         * 下面的第一个参数 post , 实际是绑定到路由中的 {post} 参数,
         * 当在路由中检测到这个参数时, 会将该参数值替换为经闭包处理后的返回值
         * 碰到的所有命名为{post}的参数, 都会被处理, 即便你不希望被处理的
         */
        $router->bind('post', function($id) {
            return \App\Post::with('hasOneUser')->find($id);
        });

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}
